package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.cgw.game.entity.RectEntity;

import java.util.ArrayList;

public class TransverseChat extends View {

    private Context mContext;

    private int width;
    private int arcWidghts;
    private int pillarsWidth = 8, chartHeight;//柱子宽度、柱子高度
    private int arcWidght;//线帽高度
    private Paint deepSleepPaint;
    private ArrayList<RectEntity> rects;

    public TransverseChat(Context context) {
        super(context);
        init(context, null);
    }

    public TransverseChat(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attributeSet) {
        mContext = context;
        initPaint();
        initData();
    }

    private void initPaint() {
        deepSleepPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        deepSleepPaint.setStyle(Paint.Style.FILL);
        deepSleepPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        deepSleepPaint.setStrokeWidth(DensityUtil.dip2px(mContext, pillarsWidth));
        arcWidght = DensityUtil.dip2px(mContext, 4);
    }

    private void initData() {
        rects = new ArrayList<>();
        int[] oneRect = new int[]{6, 4, 8, 6};
        String[] rectColors = new String[]{"#506FDC", "#7794ED", "#FFAF58", "#506FDC"};
        RectEntity rectEntity = new RectEntity(oneRect, rectColors);
        rects.add(rectEntity);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        arcWidghts = getMeasuredWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawChart(canvas);
    }

    /**
     * 画横向柱状图
     *
     * @param canvas
     */
    private void drawChart(Canvas canvas) {
        for (int i = 0; i < rects.size(); i++) {
            int[] rectPoints = rects.get(i).getPoints();
            String[] rectColors = rects.get(i).getColor();
            int max = rects.get(i).getMax();
            int widthInterval = 0;
            int width2 = widthInterval;
            //总长度需要减去一个线帽的高度。
            width = arcWidghts - arcWidght;
            for (int j = 0; j < rectPoints.length; j++) {
                if (j == 0 || j == rectPoints.length - 1) {
                    deepSleepPaint.setStrokeCap(Paint.Cap.ROUND);
                } else {
                    deepSleepPaint.setStrokeCap(Paint.Cap.SQUARE);
                }
                deepSleepPaint.setColor(Color.parseColor(rectColors[j]));
                //倒数第二个最后再画
                if (rectPoints.length - 2 == j) {
                    width2 = widthInterval;
                    widthInterval = widthInterval + width * rectPoints[j] / max;
                } else {
                    canvas.drawLine(arcWidght + widthInterval, arcWidght, widthInterval + width * rectPoints[j] / max,
                            arcWidght, deepSleepPaint);

                    widthInterval = widthInterval + width * rectPoints[j] / max;
                }

            }
            //绘制倒数第二条线
            try {
                deepSleepPaint.setStrokeCap(Paint.Cap.SQUARE);
                deepSleepPaint.setColor(Color.parseColor(rectColors[rectPoints.length - 2]));
                canvas.drawLine(arcWidght + width2, arcWidght, width2 + width * rectPoints[rectPoints.length - 2] / max,
                        arcWidght, deepSleepPaint);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 设置柱状图数据，并重新刷新视图
     */
    public void setChartData(RectEntity rectEntity) {
        rects.clear();
        rects.add(rectEntity);
        invalidate();
    }

}

